package com.github.chirspan.xaas.cfg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.chirspan.xaas.cfg.dao.CfgDictDao;
import com.github.chirspan.xaas.cfg.dao.CfgDictItemDao;
import com.github.chirspan.xaas.cfg.model.CfgDict;
import com.github.chirspan.xaas.cfg.service.ICfgDictService;
import com.github.chirspan.xaas.cfg.model.CfgDictItem;
import com.github.chirspan.xaas.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: chenpan
 * @Date: 2021-04-07 18:24:53
 * @Description: 数据字典 接口实现
 */
@Service
public class CfgDictServiceImpl extends BaseServiceImpl<CfgDictDao, CfgDict> implements ICfgDictService {

    @Autowired
    private CfgDictItemDao dictItemDao;

    @Autowired
    private CfgDictDao dictDao;

    /**
     * 根据主键id删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String id) {
        QueryWrapper<CfgDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CfgDictItem::getDictId, id);
        dictItemDao.delete(queryWrapper);
        return super.delete(id);
    }

    @Override
    public CfgDict getDict(String code) {
        QueryWrapper<CfgDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CfgDict::getDictCode, code);
        return dictDao.selectOne(queryWrapper);
    }
}